# 全局ARG定义
ARG CMD_NAME=main
ARG DIR_BUILD=/app
ARG DIR_CMD=/goapp

# 构建阶段
#FROM golang:1.20-alpine as build-stage
FROM crpi-bl659zwui4eoanj0.cn-hangzhou.personal.cr.aliyuncs.com/letthing/golang:1.20-alpine as build-stage

# 再次声明ARG，使其在本阶段可用
ARG CMD_NAME
ARG DIR_BUILD

WORKDIR ${DIR_BUILD}
ENV GOPROXY=https://goproxy.cn,direct
COPY . .
RUN rm -rf configs/ \
    && go mod download \
    && go mod verify \
    && go mod tidy
# 添加-ldflags="-s -w"参数来减小二进制文件体积
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \
    go build -v -ldflags="-s -w" -o ${CMD_NAME} .

# 生产阶段
FROM crpi-bl659zwui4eoanj0.cn-hangzhou.personal.cr.aliyuncs.com/letthing/alpine:latest-with-tzdata

# 再次声明ARG，使其在本阶段可用
ARG CMD_NAME
ARG DIR_BUILD
ARG DIR_CMD

WORKDIR ${DIR_CMD}
COPY --from=build-stage ${DIR_BUILD}/${CMD_NAME} .

EXPOSE 80
CMD ["./main"]